package cn.xiaolang.function.quartz;

import java.util.Date;
import java.util.Map;

import cn.xiaolang.common.utils.LoggerUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 
 * controller类型的任务调度Job引擎 
 * @version 1.0
 * @since JDK1.7
 * @author fuhw
 * @date 2016年3月31日 下午4:33:59
 */
public class ControllerEngineJob extends ZWJob{
	
		private static String controllerKeyName = "_controller_class";
	
		private Map<String, Object> params;
		
		public Map<String, Object> getParams() {
			return params;
		}

		public void setParams(Map<String, Object> params) {
			this.params = params;
		}

		//任务job的id设置：实例类名+调度时间
		private final String JOB_ID = this.getClass().getName()+"_"+ DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
		
		protected String getJOB_ID() {
			return JOB_ID;
		}

		@Override
		protected void doExecute(Map<String, Object> params) {
			if(params == null || params.get(controllerKeyName) == null) {
				LoggerUtils.info(ControllerEngineJob.class, "_controller_class的key值不能为空！");
				return;
			}
			
			Class<?> controllerClass = (Class<?>) params.get(controllerKeyName);
			params.remove(controllerKeyName);
			
			try {
				//实例化一个controller，然后执行这个controller的方法
				controllerClass.newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			
			super.doExecute(params);
		}
}
